﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IoTCM
{
    public class Thing
    {
        private DataItemCollection m_data;
        private ThingCollection m_things;

        public Thing(string id, string instance, string standardThingID, string name, params object[] content)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException("id is required");
            }
            if (string.IsNullOrEmpty(instance))
            {
                throw new ArgumentException("instance is required");
            }

            ID = id;
            Instance = instance;
            Name = name;

            if (string.IsNullOrEmpty(standardThingID))
            {
                this.StandardThingID = StandardThing.Unknown;
            }
            else
            {
                this.StandardThingID = standardThingID;
            }

            m_things = new ThingCollection(this);
            m_data = new DataItemCollection(this);

            Add(content);
        }

        public string ID { get; set; }
        public string Instance { get; set; }
        public string Name { get; set; }

        public string StandardThingID { get; set; }

        public ThingCollection Things
        {
            get { return m_things; }
        }

        public DataItemCollection Items
        {
            get { return m_data; }
        }

        public void Add(object content)
        {
            if (content == null) throw new ArgumentNullException();

            if (content is Thing)
            {
                m_things.Add(content as Thing);
            }
            else if (content is DataItem)
            {
                m_data.Add(content as DataItem);
            }
            else
            {
                throw new ArgumentException("Invalid content");
            }
        }

        public void Add(params object[] content)
        {
            foreach (var item in content)
            {
                Add(item);
            }
        }
    }
}
